#ifndef MONTHLYQUERY_H
#define MONTHLYQUERY_H

#include <QWidget>
#include <QDate>
#include <QDateEdit>
#include <QPushButton>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QRadioButton>
#include <QButtonGroup>
#include <QHeaderView>
#include <QList>
#include <QFileDialog>
#include <QMessageBox>
#include <QResizeEvent>
#include "database.h"

#ifdef Q_OS_WIN
#include <QAxObject>
#endif

class MainWindow;

class MonthlyQuery : public QWidget
{
    Q_OBJECT

public:
    explicit MonthlyQuery(MainWindow *parent = nullptr);
    ~MonthlyQuery();

    void refreshData();

protected:
    // 重写resizeEvent来处理窗口大小变化
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void onQueryClicked();
    void onSortExcellentClicked();
    void onSortPoorClicked();
    void onExportExcelClicked();

private:
    enum SortType
    {
        SortByExcellent,
        SortByPoor
    };

    MainWindow *mainWindow;
    Database *database;

    QDateEdit *startDateEdit;
    QDateEdit *endDateEdit;
    QPushButton *queryButton;
    QRadioButton *sortExcellentRadio;
    QRadioButton *sortPoorRadio;
    QPushButton *exportExcelButton;
    QTableWidget *resultTable;

    QList<Database::MonthlyStats> currentStats;
    SortType currentSortType;

    void setupUi();
    void loadData();
    void sortData();
    bool exportToExcel(const QString &filePath);
    void adjustTableColumns();
};

#endif // MONTHLYQUERY_H